//
//  AppDelegate.m
//  UI02_UILable_UITextFile_UIButton_UIImageView
//
//  Created by LGC on 16/3/10.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "MyLabel.h"
#import "MyTextFiled.h"
#import "MyButton.h"
#import "MyImageView.h"
@interface AppDelegate () <UITextFieldDelegate >

@end

@implementation AppDelegate

- (void)dealloc {
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    ViewController *vc = [[ViewController alloc]init];
    self.window.rootViewController = vc;
    
    [vc release];
    [_window release];
    
    [self createLabel];
    
    [self creatTextField];
    
    [self creatButton];
    
    [self creatImageView];
    return YES;

}

#pragma mark - 知识点1: UILabel
- (void)createLabel {
    
    MyLabel *labelOfUsername = [[MyLabel alloc]initWithFrame:CGRectMake(20, 50, 100, 40)text:@"用户名:"];
    [self.window addSubview:labelOfUsername];
    [labelOfUsername release];
    
    
    // 密码
    MyLabel *labelOfPassword = [[MyLabel alloc] initWithFrame:CGRectMake(20, 120, 100, 40) text:@"密码:"];
    
    [self.window addSubview:labelOfPassword];
    
    [labelOfPassword release];
    
   
}

#pragma mark - 知识点2:UITextField
- (void)creatTextField {
    
    MyTextFiled *textFieldOfName = [[MyTextFiled alloc] initWithFrame:CGRectMake(150, 50, 200, 40) placeHolder:@"请输入用户名"];
    
    [self.window addSubview:textFieldOfName];
    
    [textFieldOfName release];
    
    // 指定代理人, self此时指appDelegate对象本身.
    textFieldOfName.delegate = self;
    
    textFieldOfName.tag = 100;
    
    // 密码
    MyTextFiled *textFieldOfPassword = [[MyTextFiled alloc] initWithFrame:CGRectMake(150, 120, 200, 40) placeHolder:@"请输入密码"];
    
    [self.window addSubview:textFieldOfPassword];
    
    [textFieldOfPassword release];
    
    textFieldOfPassword.tag = 200;
    
    // 密文输入
    textFieldOfPassword.secureTextEntry = YES;
    
    // 键盘类型
    textFieldOfPassword.keyboardType = UIKeyboardTypeDefault;
    
    // 回车键style
    textFieldOfPassword.returnKeyType = UIReturnKeyJoin;
    
    textFieldOfPassword.delegate = self;
    
}

#pragma mark - 知识点3: UITextField协议方法
/** 当点击键盘的上的return按键 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    // 隐藏键盘
    [textField resignFirstResponder];

    
    return YES;
    

}

/** 文本内容发生变化, 调用此方法. */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSLog(@"%ld, %ld", range.location, range.length);
    
    // 限制输入数字 0 - 9.
    if ((string.intValue > 0 && string.intValue <= 9)||[string isEqualToString:@"0"]){
        return NO;
    }
    
    // 限制输入6个字符.
    else if(range.location > 5){
        return NO;
        
    }
    else {
        return YES;
    }
}

/** 将要开始编辑, 调用此方法 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // 获取对象.
    MyTextFiled *user = [self.window viewWithTag:100];
    
    MyTextFiled *passWord = [self.window viewWithTag:200];
    
    if (textField == passWord && user.text.length == 0 ) {
        return NO;
    }
    else {
            return YES;
        }
    
     

}

#pragma mark - 知识点4: UIButton
- (void)creatButton {
    /** 确认键 */
    MyButton *buttonOfOK = [MyButton buttonWithType:UIButtonTypeSystem];
    [self.window addSubview:buttonOfOK];
    
    /** 尺寸 */
    buttonOfOK.frame = CGRectMake(50, 250, 95, 40);
    /** 颜色 */
    buttonOfOK.backgroundColor = [UIColor redColor];
    /** 文本 */
    [buttonOfOK setTitle:@"确认" forState:UIControlStateNormal];
    
    // 字体
    buttonOfOK.titleLabel.font = [UIFont systemFontOfSize:23];
    buttonOfOK.tintColor = [UIColor greenColor];
    
    // 添加事件
    [buttonOfOK addTarget:self action:@selector(handleOK:) forControlEvents:UIControlEventTouchUpInside];
    
    
    /** 取消键 */
    MyButton *buttonOfCancel = [MyButton buttonWithType:UIButtonTypeSystem];
    [self.window addSubview:buttonOfCancel];
    
    // 设置图片
    // 背景
    [buttonOfCancel setBackgroundImage:[UIImage imageNamed:@"iconfont-quxiao.png" ] forState:UIControlStateNormal];
    // 前景
    [buttonOfCancel setImage:[UIImage imageNamed:@"iconfont-quxiao.png"] forState:UIControlStateNormal];
    // 前景和title在同一图层
    [buttonOfCancel setTitle:@"取消" forState:UIControlStateNormal];
    
    buttonOfCancel.frame = CGRectMake(250, 250, 100, 40);
    buttonOfCancel.backgroundColor = [UIColor redColor];

}

- (void)handleOK:(UIButton *)button {
    
    // 回收键盘
    [self.window endEditing:YES];// 父视图响应
    
}

#pragma mark - 知识点5:UIImageView
- (void)creatImageView {
    
    // 创建对象
    MyImageView *imageView = [[MyImageView alloc]
                              initWithImage:[UIImage imageNamed:@"iconfont-xiaogougou.png"]];
    
    imageView.frame = CGRectMake(100, 400, 200, 200);
    
    [self.window addSubview:imageView];
    
    [imageView release];
}
@end
